今天介紹 const 以及 iota
package main
import (
"fmt"
)
const two = 2
const (
Female = 2
Male = 1
)
const (
a = iota
b = iota
c = iota
)
const (
Sunday = iota
Monday
Tuesday
)
func main() {
fmt.Printf("two = %d\n", two)
fmt.Printf("Female is %d\n", Female)
fmt.Printf("Male is %d\n", Male)
fmt.Printf("a = %d b = %d c = %d\n", a, b, c)
fmt.Printf("Sunday = %d Monday = %d Tuesday = %d\n", Sunday, Monday, Tuesday)
}
執行結果如下:
two = 2
Female is 2
Male is 1
a = 0 b = 1 c = 2
Sunday = 0 Monday = 1 Tuesday = 2
可以比較方便,少打一些字.
const (
a = iota
b = iota
c = iota
)const (
Sunday = iota
Monday
Tuesday
)
這一段請解釋清楚,這禮拜其它天跑哪去了....
改用中文版補上.
<pre class="c" name="code">
package main
import (
"fmt"
)
const two = 2
const (
Female = 2
Male = 1
)
const (
a = iota
b = iota
c = iota
)
const (
Sunday = iota
Monday
Tuesday
)
const (
星期日 = iota
星期一
星期二
星期三
星期四
星期五
星期六
)
func main() {
fmt.Printf("two = %d\n", two)
fmt.Printf("Female is %d\n", Female)
fmt.Printf("Male is %d\n", Male)
fmt.Printf("a = %d b = %d c = %d\n", a, b, c)
fmt.Printf("Sunday = %d Monday = %d Tuesday = %d\n", Sunday, Monday, Tuesday)
fmt.Println()
fmt.Printf("星期日 = %d 星期一 = %d 星期二 = %d\n", 星期日, 星期一, 星期二)
fmt.Printf("星期日 = %d 星期一 = %d 星期二 = %d\n", 星期三, 星期四, 星期五)
fmt.Printf("星期六 = %d", 星期六)
}
執行結果如下: